package es.upv.tricia.activity;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import es.upv.tricia.R;

public class MainActivity extends Activity implements OnClickListener
{
	final static public int HELP_DIALOG = 1;
	final static private String DEBUG_TAG = "MainActivity";

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button b = (Button) findViewById(R.id.creditsButton);
		b.setOnClickListener(this);

		b = (Button) findViewById(R.id.scoresButton);
		b.setOnClickListener(this);

		b = (Button) findViewById(R.id.playButton);
		b.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.main, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) {
		case R.id.helpButton:
			showDialog(HELP_DIALOG);
			return true;
		case R.id.settingsButton:
			startActivity(new Intent(this, SettingsActivity.class));
			return true;
		}

		return false;
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch (id) {
		case HELP_DIALOG:

			InputStream inputStream = getResources().openRawResource(
					R.raw.help_dlg_main);
			DataInputStream dataStream = new DataInputStream(inputStream);

			try {
				String line = dataStream.readLine();
				builder.setMessage(line);
				dataStream.close();
				inputStream.close();
			} catch (IOException ioe) {
				Log.d(DEBUG_TAG, "Error while reading myFile");
			}
			break;
		default:
			throw new RuntimeException("Unknown dialog ID " + id);
		}

		return builder.create();
	}

	@Override
	public void onClick(View v)
	{
		if (v.getId() == R.id.creditsButton) {
			startActivity(new Intent(this, CreditsActivity.class));
		} else if (v.getId() == R.id.scoresButton) {
			startActivity(new Intent(this, ScoresActivity.class));
		} else if (v.getId() == R.id.playButton) {
			startActivity(new Intent(this, PlayActivity.class));
		}
	}
}